package com.saltfish.appengine.worktable;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saltfish.appengine.worktable.datastore.Work;

@SuppressWarnings("serial")
public class RegisterWorkServlet extends ActionServlet {
	@Override
	protected void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String sid = req.getParameter("id");
		String selYm = req.getParameter("selYm");
		String startTime = req.getParameter("starttime");
		String endTime = req.getParameter("endtime");
		String breakTime = req.getParameter("breaktime");
		String comment = req.getParameter("comment");
		String holiday = req.getParameter("holiday");
		
        Long id = null;
        try {
            id = Long.parseLong(sid);
        } catch (NumberFormatException e) {
            id = null;
        }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        Work work = pm.getObjectById(Work.class, id);
        work.setHoliday(holiday);
        work.setComment(comment);
        if ("on".equals(holiday)) {
            work.setStartTime(null);
            work.setEndTime(null);
            work.setBreakTime(null);
        } else {
            work.setStartTime(startTime);
            work.setEndTime(endTime);
            work.setBreakTime(breakTime);
        }
        
        try {
            work = pm.makePersistent(work);
        } finally {
            pm.close();
        }
        
        req.setAttribute("work", work);
        req.setAttribute("selYm", selYm);
        
		forward("/worktable?selYm=" + selYm);
	}
}
